{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "龙曲良老师课程笔记/04 Pytorch基础教程.ipynb",
      "provenance": [],
      "toc_visible": true,
      "authorship_tag": "ABX9TyNuB9u7+QbbfSdRmeJaQSTj",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/jinxiqinghuan/learn_pytorch/blob/main/%E9%BE%99%E6%9B%B2%E8%89%AF%E8%80%81%E5%B8%88%E8%AF%BE%E7%A8%8B%E7%AC%94%E8%AE%B0/04_Pytorch%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bjgLsCX6TzCV"
      },
      "source": [
        "# Pytorch基础教程"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cQfAbYbayQoT"
      },
      "source": [
        "导库"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Gc-Pk2aITyhi"
      },
      "source": [
        "import torch\r\n",
        "import numpy as np"
      ],
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3SzwPzGfSDiE"
      },
      "source": [
        "### 1.张量数据类型"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ougy2lb3Pa_d"
      },
      "source": [
        "a = torch.randn(2, 3)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "QQ2n7K66PfAn",
        "outputId": "f95d5d2d-d4cb-470c-f85f-4a053d83f64b"
      },
      "source": [
        "a.type "
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<function Tensor.type>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WiOkFr1nP8aG",
        "outputId": "60b39a91-32f3-480f-fcc0-1414b630bb71"
      },
      "source": [
        "type(a)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Tensor"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "m9_2dEeQbLmP"
      },
      "source": [
        "#### instance判断数据类型"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4WiFLNobPgqG",
        "outputId": "75f9fbad-17f9-402f-c113-51dca857d4f9"
      },
      "source": [
        "isinstance(a, torch.FloatTensor)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "NTmWAal4Pt-K",
        "outputId": "3816f496-8d60-43bb-d5bb-d0faaa2a7ebc"
      },
      "source": [
        "isinstance(a, torch.cuda.FloatTensor)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sKVWKSvMQRMH"
      },
      "source": [
        "a = a.cuda() #使用GPU"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bfMvotuPQUF6",
        "outputId": "f8e2aaf1-05e8-491c-c707-8bedc1256835"
      },
      "source": [
        "isinstance(a, torch.cuda.FloatTensor)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Amtw-AwPQrre",
        "outputId": "fc785688-2cd7-490f-b7d9-f2f079524d36"
      },
      "source": [
        "torch.tensor(1.)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(1.)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JVdxcW-iRQ4S",
        "outputId": "064ad238-c16d-47e7-c192-34bedae49eab"
      },
      "source": [
        "torch.tensor(1.3)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(1.3000)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "e5uLvD0BRTy6"
      },
      "source": [
        "a = torch.tensor(2.2)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "pd4Fbzh8bUxH"
      },
      "source": [
        "#### 标量是0维的"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Y7dXjkRURrTV",
        "outputId": "a8619dfc-d846-42c1-c574-bf21f3253c7e"
      },
      "source": [
        "a.shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "6zsXJfLARsSm",
        "outputId": "0ab402a6-dd1a-4bdc-99eb-aee7da33f286"
      },
      "source": [
        "len(a.shape) # 标量是0维的"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "gSsvENDyTE93",
        "outputId": "7c944246-36f4-474f-e979-d9068ee2ae3e"
      },
      "source": [
        "a = torch.tensor([1.])\r\n",
        "a"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1.])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 37
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Ebf7vIkSTO_X",
        "outputId": "7d96d527-666f-4da1-e755-54963c63c93c"
      },
      "source": [
        "len(a)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 40
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WreRw9RKRue2",
        "outputId": "b923a54f-2675-4dc4-ac40-0d2db555f880"
      },
      "source": [
        "a.size"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<function Tensor.size>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 38
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "z6KqlsCRRzo3",
        "outputId": "224bc2b0-a93e-4015-c3ed-e4d2750a7f17"
      },
      "source": [
        "torch.tensor([1,1])"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 26
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "VHEZE1HESa7s",
        "outputId": "44278d81-e86b-42ca-9a8e-1dad1f3fb575"
      },
      "source": [
        "a = torch.tensor([1.1, 2.2])\r\n",
        "a"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1.1000, 2.2000])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 29
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "NSM4-SgySgVk",
        "outputId": "ecb80ff4-6e5a-4a43-98c2-bf649ecb3eaa"
      },
      "source": [
        "torch.FloatTensor(1)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([-1.2204])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 34
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AWFYl2_rbb3C"
      },
      "source": [
        "#### 指定类型和维度初始化"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "SVNn_msTSr5D",
        "outputId": "9decd260-c7ec-45a9-8cf1-2c62c2231ce3"
      },
      "source": [
        "torch.FloatTensor(2, 3) # 随机初始化指定维度的tensor"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[[-1.2206,  0.0000],\n",
              "         [ 0.0000,  0.0000],\n",
              "         [ 0.0000,  0.0000]],\n",
              "\n",
              "        [[ 0.0000,  0.0000],\n",
              "         [ 0.0000,  0.0000],\n",
              "         [ 0.0000,  0.0000]]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 42
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "HgiVZYWlTW2e",
        "outputId": "4eabfa3d-e485-47ce-e5f7-ec428535fa52"
      },
      "source": [
        "data = np.ones(2)\r\n",
        "data"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([1., 1.])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 50
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "HLIADuj-Tv0v",
        "outputId": "1110e689-5431-4de4-9ab3-516df4e84465"
      },
      "source": [
        "data = torch.from_numpy(data)\r\n",
        "data"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1., 1.], dtype=torch.float64)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 54
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_Gk0gSkJWIa6"
      },
      "source": [
        "#### 一维的tensor可以用在哪里？\r\n",
        "1. bias偏置\r\n",
        "2. [28, 28]->[784] batch=1的手写数据集\r\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-voGl7SwWH03",
        "outputId": "bc197290-f427-4116-e3ce-d533ed13610f"
      },
      "source": [
        "print(data)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([1., 1.], dtype=torch.float64)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "OQD9kLeqU6qo"
      },
      "source": [
        "a = torch.ones(2)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "EqhiGJICXPVH",
        "outputId": "9a638c99-1eee-46fc-b4d7-660c4db64da2"
      },
      "source": [
        "a, a.shape, a.size"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(tensor([1., 1.]), torch.Size([2]), <function Tensor.size>)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 58
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jOAt2FU3XQ9x"
      },
      "source": [
        "a = torch.randn(2, 3) # 随机正态分布"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "xvbxe-RSXsBI",
        "outputId": "749848d3-b873-4f21-fc6f-8da25ae7d9c4"
      },
      "source": [
        "a"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-0.7167, -0.3013, -0.9241],\n",
              "        [ 0.5277,  2.5219, -1.2190]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 60
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kvYVqfQ5XsWk",
        "outputId": "86beb789-37cb-47a2-cc69-6882c4a55a14"
      },
      "source": [
        "a.shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([2, 3])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 61
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "m5LAb9dJXvYe",
        "outputId": "617cf6c1-0979-42c7-ae2b-4461c7de405c"
      },
      "source": [
        "a.size(0) # 返回shape的第一个元素"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "2"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 63
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "iouFhhnCXxBt",
        "outputId": "66b64159-5dab-4848-cbf0-5f1ebc007ce3"
      },
      "source": [
        "a.size(1) # 返回shape的第二个元素"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "3"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 64
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zO_eaqjSYSlq"
      },
      "source": [
        "#### 维度为2的使用场景:\r\n",
        "[784] -> [4, 784]"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "eFqOzQVOX3aL",
        "outputId": "108aa522-d1fa-4bbe-e94d-00351d8cdfb5"
      },
      "source": [
        "a = torch.rand(1, 2, 3)\r\n",
        "a"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[[0.7132, 0.6101, 0.2299],\n",
              "         [0.7959, 0.6889, 0.6648]]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 67
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bC9UTSlqY0JJ",
        "outputId": "9d11f279-a6e5-4dd5-b1f8-654c8df13e7e"
      },
      "source": [
        "a.shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([1, 2, 3])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 68
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "7WX-bjHgZAm4",
        "outputId": "5fa5782c-ca54-43fa-db0c-94ecdd3c1de4"
      },
      "source": [
        "a[0]"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.7132, 0.6101, 0.2299],\n",
              "        [0.7959, 0.6889, 0.6648]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 69
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "IDS21FJqZCAw",
        "outputId": "9ba1326a-76a1-4626-c281-e6ccdefe88c9"
      },
      "source": [
        "list(a.shape) # 使用python的list方法，把返回的torch.Size直接转化成list类型"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 3]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 71
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cXWl_k4Dbp5h"
      },
      "source": [
        "#### 使用torch.rand()进行初始化"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mdOMcswlZEYz",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "57f16181-087c-497a-beb4-a4602e51cc30"
      },
      "source": [
        "a = torch.rand(2, 3, 28, 28) # [b, c, h , w] b.第几张照片， c.通道数，  h,w.长*宽\r\n",
        "a.shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([2, 3, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 74
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IR2btK17b2s1"
      },
      "source": [
        "#### numel返回占用内存大小"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ga1A1rvTagCd",
        "outputId": "ecf1ba84-d341-4bc1-f09f-d8aa2d226383"
      },
      "source": [
        "a.numel() # 2 * 3 * 28 * 28"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "4704"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 75
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "O9PycHwFbzog",
        "outputId": "7b498825-7ebc-48f9-85fb-52853a8fec15"
      },
      "source": [
        "2 * 3 * 28 * 28"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "4704"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 77
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "XL5gVXsIcNU7",
        "outputId": "a29de696-c5e8-4726-f4d4-aa9a6617c667"
      },
      "source": [
        "a.dim()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "4"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 82
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nl4ynH6-cYOM"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qBuv71NZdUiy"
      },
      "source": [
        "### 2.创建Tensor"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "PGNU0kb4dcBp",
        "outputId": "7ae36d0a-1cf6-45a2-a34c-d625d229ac14"
      },
      "source": [
        "a = np.array([2, 3.3])\r\n",
        "a"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([2. , 3.3])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 84
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ngKU17rsdgTW",
        "outputId": "d1a49a90-2892-4241-cd37-b0fe886a3100"
      },
      "source": [
        "torch.from_numpy(a)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([2.0000, 3.3000], dtype=torch.float64)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 85
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kYe5-WandojN",
        "outputId": "e6aa01ad-0fd3-4902-a43c-10f70880dd2e"
      },
      "source": [
        "a = np.ones([2,3])\r\n",
        "torch.from_numpy(a)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 1., 1.],\n",
              "        [1., 1., 1.]], dtype=torch.float64)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 87
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VZLmihu9eom-"
      },
      "source": [
        "#### torch.tensor()和torch.FloatTensor()\r\n",
        "一般情况下，大写的Tensor接收的是数据的维度，而小写的接收的是数据"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fW2CxJzId3_S",
        "outputId": "a0350580-2fca-4895-cfa8-2925dde143ac"
      },
      "source": [
        "torch.tensor([2., 3.2])"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([2.0000, 3.2000])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 90
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-e4rAw-neMvM",
        "outputId": "6f4da8a0-fb8c-40b3-9498-d360a5addbef"
      },
      "source": [
        "torch.FloatTensor([2., 3.2])"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([2.0000, 3.2000])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 91
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "aMdORfUmeTq8",
        "outputId": "6c88f5be-327c-4c8a-b177-a36b1c5cbd5f"
      },
      "source": [
        "torch.tensor([[2., 3.2], [1., 22.3]])"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 2.0000,  3.2000],\n",
              "        [ 1.0000, 22.3000]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 92
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0ba6SKMVgDMt"
      },
      "source": [
        "#### 生成未初始化的数据\r\n",
        "* torch.empty()\r\n",
        "* torch.FloatTensor(d1, d2, d3)\r\n",
        "* torch.IntTensor()\r\n",
        "\r\n",
        "未初始化的tensor一定要跟写入数据的后续步骤, 只是暂时作为一个容器"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5FWxr2f7ec1W",
        "outputId": "611934aa-6510-4a7d-a66a-93c0bea5dead"
      },
      "source": [
        "torch.empty(1)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([-1.2203])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 99
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8-nnXtV4fL2Z",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "a0216b09-9f43-4a34-90a6-41cae27b6aa1"
      },
      "source": [
        "torch.Tensor(2, 3) # 生成的数据非常不规则"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.2203e+00,  0.0000e+00,  1.5975e-43],\n",
              "        [ 1.3873e-43,  1.4574e-43,  6.4460e-44]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 100
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "u3FTzfAnf4h1",
        "outputId": "9c014c2f-f860-47e4-f1d5-7e04ce0643fe"
      },
      "source": [
        "torch.IntTensor(2, 3) "
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1080282112,           0,          24],\n",
              "        [          0,          -1,          46]], dtype=torch.int32)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 101
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "OsKNlwzQhatw",
        "outputId": "c1a24c77-e7a0-432d-aef5-dfb327739982"
      },
      "source": [
        "torch.FloatTensor(2, 2)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.2203e+00,  0.0000e+00],\n",
              "        [ 1.5975e-43,  1.3873e-43]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 104
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vhDzpiokh-Ua"
      },
      "source": [
        "#### 设置默认类型"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "2mkwSIBbhqUc",
        "outputId": "8778ac80-b283-43c4-eb63-9e680c535b6b"
      },
      "source": [
        "torch.tensor([1.2, 3]).type() # pytorch默认类型,更长使用Double类型"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            },
            "text/plain": [
              "'torch.FloatTensor'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 106
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MAO8AzONh5Lu"
      },
      "source": [
        "torch.set_default_tensor_type(torch.DoubleTensor)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "OlcDuxDDiQE4",
        "outputId": "16186d52-25ce-4b98-ee32-97492b2ccaa9"
      },
      "source": [
        "torch.tensor([1.2, 3]).type()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            },
            "text/plain": [
              "'torch.DoubleTensor'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 108
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YrBstsBqiZr9"
      },
      "source": [
        "#### torch常用的随机初始化"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "OnZeTll5ifm1",
        "outputId": "ccd3b68d-8385-4ba6-fb99-49b5416194e4"
      },
      "source": [
        "torch.rand(3, 3)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.7864, 0.1525, 0.1233],\n",
              "        [0.0594, 0.9376, 0.4399],\n",
              "        [0.3681, 0.5145, 0.8572]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 111
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RTxM68zEikkL",
        "outputId": "21bc3f57-9d45-4b9c-d9d3-f7b3a7c907f9"
      },
      "source": [
        "a = torch.rand(3, 2)\r\n",
        "a"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.9234, 0.9789],\n",
              "        [0.7305, 0.5540],\n",
              "        [0.9511, 0.6229]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 112
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "j-cM_SfDi_vR"
      },
      "source": [
        "#### 编程tips\r\n",
        "在最近研究的GAN项目中，这里在成对图像的生成任务中，可以使用：生成图像 = torch.rand_like(原始图像) 生成噪声来进行输入"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "BzENq0B9ix-W",
        "outputId": "a1e6f256-5a22-47ee-ca79-ea6929ce38c1"
      },
      "source": [
        "torch.rand_like(a) "
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.8654, 0.5623],\n",
              "        [0.4267, 0.6323],\n",
              "        [0.4710, 0.6784]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 113
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4yRXuYJfi2A8",
        "outputId": "818c5163-bc52-40c5-ec11-877e37c2e734"
      },
      "source": [
        "torch.randint(1, 10, [3, 3]) # 参数：最小值， 最大值， shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[5, 6, 6],\n",
              "        [1, 7, 1],\n",
              "        [4, 2, 5]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 119
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TcMqdh3Kr14u",
        "outputId": "6b3e3b2f-3b87-4108-c821-67d6549ac721"
      },
      "source": [
        "torch.randn(3, 3)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0.2031,  0.6228, -0.5930],\n",
              "        [-1.6114, -1.2220, -1.2801],\n",
              "        [ 0.7028,  0.9088, -0.3470]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 120
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4k_ER4U-sZds"
      },
      "source": [
        "# torch.normal(mean=torch.full([10],0), std=torch.arange(1, 0, -0.1))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LvQ9WnnaspOQ",
        "outputId": "36b0d9f3-ffb6-4dd5-a1f3-09f53c31ce72"
      },
      "source": [
        "torch.full([2,3], 7)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[7, 7, 7],\n",
              "        [7, 7, 7]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 123
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "cNb7oS9LtdG0",
        "outputId": "97b19b9a-1458-4ab2-e9c1-cb7e8716f422"
      },
      "source": [
        "torch.full([], 7)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(7)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 124
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "m32KnW75tjd1",
        "outputId": "fd9468da-2ac0-41ce-f15b-5c9434e36a60"
      },
      "source": [
        "torch.arange(0, 10)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 125
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "P_MnM3c_yeXD",
        "outputId": "6573f6ae-d8a4-4125-98b8-299f0438b595"
      },
      "source": [
        "torch.arange(0, 10, 2)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([0, 2, 4, 6, 8])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 126
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "xjoT0BAcyiyQ",
        "outputId": "09b5b0f7-8486-4abd-dfa8-b45d2f8ab1e3"
      },
      "source": [
        "torch.range(0, 10) # 不建议使用"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: UserWarning: torch.range is deprecated and will be removed in a future release because its behavior is inconsistent with Python's range builtin. Instead, use torch.arange, which produces values in [start, end).\n",
            "  \"\"\"Entry point for launching an IPython kernel.\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 128
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "u5nwSiL70i1J"
      },
      "source": [
        "#### torch.linspace()"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "012q3BMbyoSb",
        "outputId": "e69ea44b-b344-4412-cf6f-9f2754a652dd"
      },
      "source": [
        "torch.linspace(0, 10, steps=4)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([ 0.0000,  3.3333,  6.6667, 10.0000])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 130
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JiQRBK5a0dWf",
        "outputId": "23366b9e-d443-4c70-afc1-e02bb1d0496c"
      },
      "source": [
        "torch.linspace(0, 10, steps=10)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([ 0.0000,  1.1111,  2.2222,  3.3333,  4.4444,  5.5556,  6.6667,  7.7778,\n",
              "         8.8889, 10.0000])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 131
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fiasGYUc0sG3",
        "outputId": "fd95b049-f611-4f5f-bcc9-01190e1b88e3"
      },
      "source": [
        "torch.linspace(0, 10, steps=11)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 134
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "PbLY9LRU1D9r",
        "outputId": "d2cca9b5-a2ec-43cb-cf93-57655f6e2849"
      },
      "source": [
        "torch.logspace(0, -1, steps=10)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1.0000, 0.7743, 0.5995, 0.4642, 0.3594, 0.2783, 0.2154, 0.1668, 0.1292,\n",
              "        0.1000])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 135
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "S8lnekOW3D93"
      },
      "source": [
        "#### 0,1,单位矩阵"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "sUfpUWp82DAZ",
        "outputId": "a6e46bf9-86a0-433c-f26f-dd13f298d2f8"
      },
      "source": [
        "torch.ones(3, 3)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 1., 1.],\n",
              "        [1., 1., 1.],\n",
              "        [1., 1., 1.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 136
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5LAIUOp33OGi",
        "outputId": "828fc92a-f381-4140-b078-ec3bc03489b0"
      },
      "source": [
        "torch.zeros(3, 3)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0., 0., 0.],\n",
              "        [0., 0., 0.],\n",
              "        [0., 0., 0.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 137
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bawQW8EA3RrI",
        "outputId": "7afb720e-1042-400c-9deb-80fdb0d07325"
      },
      "source": [
        "torch.eye(3, 4)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 0., 0., 0.],\n",
              "        [0., 1., 0., 0.],\n",
              "        [0., 0., 1., 0.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 138
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "sR5DZl593Uu5",
        "outputId": "675ec0c1-3a90-4393-f365-949895c26178"
      },
      "source": [
        "torch.eye(3, 3)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 0., 0.],\n",
              "        [0., 1., 0.],\n",
              "        [0., 0., 1.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 139
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "OBP3EbH84Bs-"
      },
      "source": [
        "#### random.shuffle"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "uyVw1j053bv5",
        "outputId": "3a66bc51-e81c-4992-a184-f6fc7d9cdbc7"
      },
      "source": [
        "torch.randperm(10)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([8, 2, 7, 0, 4, 5, 1, 9, 3, 6])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 140
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zC2vStTi4HRh"
      },
      "source": [
        "a = torch.rand(2, 3)\r\n",
        "b = torch.rand(2, 2)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Uv4w3ZQb4LiN",
        "outputId": "8c0cda74-1070-4d9a-fcdf-b3b70a5b5ecb"
      },
      "source": [
        "idx = torch.randperm(2)\r\n",
        "idx"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([0, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 186
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9zHC8xY24PUA",
        "outputId": "c78ee164-1279-4434-b5f1-ff1d4d66d5d0"
      },
      "source": [
        "idx"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([0, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 187
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0kp5FKM54Pzw",
        "outputId": "df339385-877d-4a8c-81f0-25dc2f9525ae"
      },
      "source": [
        "a[idx]"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.0734, 0.4379, 0.2059],\n",
              "        [0.4562, 0.1850, 0.1916]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 188
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TP1IPNhY4suE",
        "outputId": "57aadf5c-708b-47ce-9ad6-7a0467255055"
      },
      "source": [
        "b[idx]"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.1709, 0.7325],\n",
              "        [0.0880, 0.3170]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 189
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "AmWk8XGm4ubi",
        "outputId": "b181e00e-a896-4dd5-fdc0-e3770767ecc6"
      },
      "source": [
        "a, b"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(tensor([[0.0734, 0.4379, 0.2059],\n",
              "         [0.4562, 0.1850, 0.1916]]), tensor([[0.1709, 0.7325],\n",
              "         [0.0880, 0.3170]]))"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 190
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qjorqSqO-bUj"
      },
      "source": [
        "### 3.索引与切片"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "k3DX5nShD9jM"
      },
      "source": [
        "#### 索引规则： start : end : step"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "rkvafUeG4v7-",
        "outputId": "d5b318ce-409d-4e21-b0a2-77011565deda"
      },
      "source": [
        "a = torch.rand(4, 3, 28, 28)\r\n",
        "a[0].shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([3, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "VPhZnTSj-woe",
        "outputId": "06b3d166-0ef2-49c9-dcc1-7d58681e0a4c"
      },
      "source": [
        "a[0, 0].shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "UITV569z-_5e",
        "outputId": "42f0aab7-befb-4430-ea76-b4f3d08f6d2a"
      },
      "source": [
        "a[0, 0, 2, 4]"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(0.8190)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9AKhOrcA_Lrj",
        "outputId": "456346f7-e431-4ba3-c014-766bc7a8afb4"
      },
      "source": [
        "a[:2].shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([2, 3, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "h-dt3mdZ_lQn",
        "outputId": "fa62f748-e7ad-47fd-c669-44c131bc4286"
      },
      "source": [
        "a[:2, :1, :, :].shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([2, 1, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "b8lDyvSA_swi",
        "outputId": "958bb766-fc9f-4ed1-c8fe-626e3b522360"
      },
      "source": [
        "a[:2, :-1, :, :].shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([2, 2, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "OqL7PCB9_z2X",
        "outputId": "257398fe-9547-4e9e-dc25-d23d54a4e4d8"
      },
      "source": [
        "a[:, :, 0:28:2, 0:28:2].shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 3, 14, 14])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "J6kEU4MTAUCU",
        "outputId": "5213d4e9-6fb5-43b0-f310-82e5defbeea2"
      },
      "source": [
        "a[:, :, ::2, ::2].shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 3, 14, 14])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "uKd0I7dnAhpn",
        "outputId": "0122b2a9-bc21-49b8-8a6d-7d6c688db566"
      },
      "source": [
        "a.shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 3, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6w1YtEPXGCy2"
      },
      "source": [
        "#### index_select()\r\n",
        " 参数：选取的维度， 做出的改变（必须是torch.tensor）"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0htqyfe1AnJ9",
        "outputId": "218e9547-e653-4e09-e577-f2640c1594f0"
      },
      "source": [
        "a.index_select(0, torch.tensor([0, 2])).shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([2, 3, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "dIypg9adDylh",
        "outputId": "4e3dff45-b515-48aa-ec39-38c1f91ec661"
      },
      "source": [
        "a.index_select(0, torch.tensor([1 ,2])).shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([2, 3, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "vadCcwnAFBtl",
        "outputId": "1048913f-dbce-440c-9e27-8fca9aaa66ba"
      },
      "source": [
        "a.index_select(2, torch.arange(28)).shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 3, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "1q-UsGzxFiFS",
        "outputId": "72591bc4-9a50-4569-9cf0-f9a2208c3530"
      },
      "source": [
        "a.index_select(2, torch.arange(8)).shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 3, 8, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rjZm-_eOHVSM"
      },
      "source": [
        "#### 使用...来简化索引"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "f0i1ucTUFoHM",
        "outputId": "e313b7df-fde4-45a1-cbf6-95d439edd788"
      },
      "source": [
        "a[...].shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 3, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Ir5OSNA0Gnm6",
        "outputId": "3bf4d0e4-f829-46ea-fd8b-e298e761b390"
      },
      "source": [
        "a[0, ...].shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([3, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Il2vUVbCGuTv",
        "outputId": "14deb193-76a3-493c-b464-e352c82ea647"
      },
      "source": [
        "a[:, 1, ...].shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "o-wlWx5_Gw7I",
        "outputId": "9bd066a4-6757-4c09-fd75-dedfbdda0e5b"
      },
      "source": [
        "a[..., :2].shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 3, 28, 2])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 26
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "u4rb7ETTHIjR",
        "outputId": "1e9fb7f2-38df-4161-bc27-07457e22dd19"
      },
      "source": [
        "x = torch.randn(3, 4)\r\n",
        "x"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 1.1545,  0.5730,  0.0829, -1.1386],\n",
              "        [ 0.2590,  0.0364, -0.5729,  0.2742],\n",
              "        [-1.0594,  0.8124,  0.7833,  0.0774]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 33
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5IJYjLagHg7H"
      },
      "source": [
        "mask = x.ge(0.5)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "YovfVYQEHo22",
        "outputId": "9640b043-2eae-437a-f6a9-32cc2fcf71f8"
      },
      "source": [
        "mask"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ True,  True, False, False],\n",
              "        [False, False, False, False],\n",
              "        [False,  True,  True, False]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 35
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MT1yd0AXHpjh",
        "outputId": "6b127f49-fe01-4da9-bdad-9db2ddad7c07"
      },
      "source": [
        "torch.masked_select(x, mask)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1.1545, 0.5730, 0.8124, 0.7833])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 36
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vi7eJRDSH3W8"
      },
      "source": [
        "src = torch.tensor([[4, 3, 5], [6, 7, 8]])"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TOdR1ZpTIo5l",
        "outputId": "6f926f50-03df-423d-f290-7122bb1c783f"
      },
      "source": [
        "src"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[4, 3, 5],\n",
              "        [6, 7, 8]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 39
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "w6PP4P8ZIqGf",
        "outputId": "0001391c-98d7-4aa7-b24c-7be4ebb23015"
      },
      "source": [
        "torch.take(src, torch.tensor([0, 2, 5])) # 打平（2*3->6）再取编码"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([4, 5, 8])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 41
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ytvtFuiFJUke"
      },
      "source": [
        "### 4.维度变换"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "306EWxhTJZIx"
      },
      "source": [
        "* view/reshape\r\n",
        "* squeeze/unsqueeze\r\n",
        "* transpose/ t / permute\r\n",
        "* expand/repeat"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yg5NfA9QisRj"
      },
      "source": [
        "#### view/reshape"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HIpzmKeRJXl_",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "8a4ec057-0943-4864-b882-5f196078f6cb"
      },
      "source": [
        "a = torch.rand(4, 1, 28, 28)\r\n",
        "a.shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 1, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lki9dy-MCC0R",
        "outputId": "5c11a604-1ebf-40e0-d809-a9d7ab10231b"
      },
      "source": [
        "a.view(4, 28*28)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.9446, 0.0023, 0.0423,  ..., 0.0347, 0.2917, 0.1911],\n",
              "        [0.2472, 0.0074, 0.6001,  ..., 0.7573, 0.8983, 0.1486],\n",
              "        [0.3728, 0.8783, 0.7828,  ..., 0.4515, 0.7450, 0.4672],\n",
              "        [0.5256, 0.7241, 0.7245,  ..., 0.6466, 0.6540, 0.7789]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "uwBguyfZFmRS",
        "outputId": "36969e54-ef77-43ed-9f53-576c6979737e"
      },
      "source": [
        "a.view(4, 28*28).shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 784])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jdSo-CZZF8hj",
        "outputId": "5b4b6091-bd60-44eb-eae6-d8321c17edad"
      },
      "source": [
        "a.view(4*28, 28).shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([112, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "cg1jpOTzGLEf",
        "outputId": "01238d6e-7092-4bc3-9ff1-a76ba59342b2"
      },
      "source": [
        "a.view(4, 28, 28).shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "H9TwoAj-GQJQ",
        "outputId": "f821455d-826a-47c8-b71f-b549d801cfac"
      },
      "source": [
        "b = a.view(4, 784)\r\n",
        "b.shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 784])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Jd4rWWNMGViI",
        "outputId": "957932a4-1aef-4fee-9d51-44cce9be9c8c"
      },
      "source": [
        "b.view(4, 28, 28, 1).shape # 数据污染"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 28, 28, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "v3vna585jAts"
      },
      "source": [
        "#### squeeze/unsqueeze"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "qPN6gRIokZwC",
        "outputId": "881aed90-6492-4295-c143-45e164e95726"
      },
      "source": [
        "a = torch.rand(4, 1, 28, 28)\r\n",
        "a.shape"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 1, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "cWDQ2pdNjJOA",
        "outputId": "2198b980-f7ea-4acb-9897-8f51924e12d9"
      },
      "source": [
        "a.unsqueeze(0).shape"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([1, 4, 1, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RxwEcEqPjNBE",
        "outputId": "cb73b44b-82dd-4a8f-f29d-023cc0b9e87a"
      },
      "source": [
        "a.unsqueeze(-1).shape"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 1, 28, 28, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "sGuMc3bejQlu",
        "outputId": "026879b9-db8c-4ea9-9249-d94126f1155a"
      },
      "source": [
        "a.unsqueeze(4).shape"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 1, 28, 28, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Acyc5F05jjDH",
        "outputId": "c5b4122c-be3f-490c-9980-38d83bf419ea"
      },
      "source": [
        "a.unsqueeze(-4).shape"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 1, 1, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "rQztMbc1jmgc",
        "outputId": "8485d2cb-342c-4934-8dd9-ca9617ad41b7"
      },
      "source": [
        "a.unsqueeze(-5).shape"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([1, 4, 1, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "QI0MRH3kjqKQ",
        "outputId": "de0670ce-c137-415e-aec9-bd412a270f5d"
      },
      "source": [
        "a = torch.tensor([1.2, 2.3])\r\n",
        "a"
      ],
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1.2000, 2.3000])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jlk1jKf6j28C",
        "outputId": "dacabe5d-87a2-4a12-a1d6-ed39cf72ceeb"
      },
      "source": [
        "a.unsqueeze(-1)"
      ],
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1.2000],\n",
              "        [2.3000]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PODd5zNEmcqe",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "daf4d263-49a8-4d1f-86e0-3414f0a79717"
      },
      "source": [
        "a.unsqueeze(-1).shape"
      ],
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([2, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "m4VPY9zRmeJJ",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "c6d99b54-9ee9-44e9-b395-4da28d382095"
      },
      "source": [
        "a.unsqueeze(0), a.unsqueeze(0).shape"
      ],
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(tensor([[1.2000, 2.3000]]), torch.Size([1, 2]))"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FGRunubzo68l"
      },
      "source": [
        "#### example"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "h61zMPBBolrG",
        "outputId": "0019a947-a004-4b12-e1ea-94c74961fcef"
      },
      "source": [
        "b = torch.rand(32)\r\n",
        "b"
      ],
      "execution_count": 39,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([0.6816, 0.1510, 0.8587, 0.5758, 0.6489, 0.4191, 0.2799, 0.0742, 0.8231,\n",
              "        0.4537, 0.4498, 0.4146, 0.2266, 0.1257, 0.7034, 0.0301, 0.9404, 0.5280,\n",
              "        0.1027, 0.5949, 0.9248, 0.0259, 0.3613, 0.5181, 0.9873, 0.3825, 0.7440,\n",
              "        0.3486, 0.7091, 0.2414, 0.0246, 0.3328])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 39
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "tSm3kyQRpAfH",
        "outputId": "bc58e446-9a16-4426-e10f-10dda8d2950a"
      },
      "source": [
        "f = torch.rand(4, 32, 14, 14)\r\n",
        "f.shape"
      ],
      "execution_count": 40,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 32, 14, 14])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 40
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_0n6YefDpGH3",
        "outputId": "e729b70d-e707-4a49-e620-3262afb441d5"
      },
      "source": [
        "b = b.unsqueeze(1).unsqueeze(2).unsqueeze(0)\r\n",
        "b.shape"
      ],
      "execution_count": 41,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([1, 32, 1, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 41
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZkS5AAZEp7kq"
      },
      "source": [
        "#### squeeze"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "SReHRK21pQsl",
        "outputId": "d91e892e-c278-47e5-ab3d-136fdda2e904"
      },
      "source": [
        "b.shape"
      ],
      "execution_count": 42,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([1, 32, 1, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 42
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ALg08qQ7phNN",
        "outputId": "b61b5c03-f8f7-4c68-95bf-028be68f71de"
      },
      "source": [
        "b.squeeze().shape"
      ],
      "execution_count": 43,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([32])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 43
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9k7BSZQeqBKw",
        "outputId": "58fc3bd7-fafe-49ff-9f87-3181b94ca59a"
      },
      "source": [
        "b.squeeze(0).shape"
      ],
      "execution_count": 44,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([32, 1, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 44
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "cLKsiD6pqEBZ",
        "outputId": "37f9d546-e8a3-462c-a040-dee44c55fb7f"
      },
      "source": [
        "b.squeeze(-1).shape"
      ],
      "execution_count": 45,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([1, 32, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 45
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0WaBUIvVqOTx",
        "outputId": "8f809e2f-4afd-415d-bf37-a56e05c2ee24"
      },
      "source": [
        "b.squeeze(1).shape"
      ],
      "execution_count": 46,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([1, 32, 1, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 46
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "BONKeq02qP9p",
        "outputId": "6ced8ff1-32d6-45ca-d3a3-3a122ba192c6"
      },
      "source": [
        "b.squeeze(-4).shape"
      ],
      "execution_count": 47,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([32, 1, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 47
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9RyvvZkzqVY6"
      },
      "source": [
        "#### expand/repeat\r\n",
        "* expand: broadcasting(推荐)\r\n",
        "* repeat: memory copied"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xn8xjbEAqRp3"
      },
      "source": [
        "a = torch.rand(4, 32, 14, 14)"
      ],
      "execution_count": 48,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LQTMrJl9rKxD",
        "outputId": "4928d776-bc82-4f1c-ddcc-43283808c9e8"
      },
      "source": [
        "b.shape"
      ],
      "execution_count": 49,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([1, 32, 1, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 49
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Ggocn3_PrLnX",
        "outputId": "db872e58-4ed2-4945-a12f-649fdc149d23"
      },
      "source": [
        "b.expand(4, 32, 14, 14).shape"
      ],
      "execution_count": 51,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 32, 14, 14])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 51
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9DwZYkIirRZ6",
        "outputId": "617d8254-f78f-404e-a590-18d5a6500011"
      },
      "source": [
        "b.expand(-1, 32, -1, -1).shape"
      ],
      "execution_count": 52,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([1, 32, 1, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 52
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pNmuoFNHra3D",
        "outputId": "f4b55dee-bb91-4eb1-865d-279dc7b8ddb2"
      },
      "source": [
        "b.expand(-1, 32, -1, -4).shape"
      ],
      "execution_count": 53,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([1, 32, 1, -4])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 53
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "qN1_R4rprijv",
        "outputId": "0b2c696f-7aae-49a1-8d8c-7589cbc2d276"
      },
      "source": [
        "b.shape"
      ],
      "execution_count": 55,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([1, 32, 1, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 55
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "83mklUYsru32",
        "outputId": "1a23d3e2-57d3-465f-c79b-85ec34cfacb2"
      },
      "source": [
        "b.repeat(4, 32, 1, 1).shape"
      ],
      "execution_count": 56,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 1024, 1, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 56
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SVfYt9qVtrx5"
      },
      "source": [
        "#### 转置和维度交换"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4cqzJ6mBtzEV"
      },
      "source": [
        "t()转置函数，只能用在二维的矩阵上"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "IE5VHp0LsSTV"
      },
      "source": [
        "a = torch.randn(3, 4)"
      ],
      "execution_count": 61,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "or9EKRTHtg5K",
        "outputId": "310cd926-5908-405a-b709-3189a5044e90"
      },
      "source": [
        "a.t().shape"
      ],
      "execution_count": 62,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 3])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 62
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1dVaqRfTuA50"
      },
      "source": [
        "transpose()"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yh9-kW5BtmVN",
        "outputId": "2b95816e-d36f-43d9-c773-a78aa334f82a"
      },
      "source": [
        "a = torch.rand(4, 3, 32, 32)\r\n",
        "a.shape"
      ],
      "execution_count": 66,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 3, 32, 32])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 66
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "gvM-Iki-t7fH",
        "outputId": "7cbd8a8b-33ee-4c10-a7a4-1c69c9863a99"
      },
      "source": [
        "a1 = a.transpose(1, 3).contiguous().view(4, 3*32*32).view(4, 3, 32, 32) # 维度信息丢失，数据造成了污染\r\n",
        "a1.shape"
      ],
      "execution_count": 69,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 3, 32, 32])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 69
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "oAhAbtvSuXEN",
        "outputId": "d6044640-8bd7-4746-ccbf-f4bd13295d3c"
      },
      "source": [
        "a2 = a.transpose(1, 3).contiguous().view(4, 3*32*32).view(4, 32, 32, 3).transpose(1, 3)\r\n",
        "a2.shape"
      ],
      "execution_count": 72,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 3, 32, 32])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 72
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "suYC3_QivnmD"
      },
      "source": [
        "#### Tips:判断两个tensor所有信息是否一致"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mCijhXC2vTZQ",
        "outputId": "74a9f093-ac1e-4a24-dfc3-1afa4f3d5cfe"
      },
      "source": [
        "torch.all(torch.eq(a, a1))"
      ],
      "execution_count": 73,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(False)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 73
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WDc8CYn7veJO",
        "outputId": "ca037ea7-dbfc-420b-f2c3-9a341a30ede7"
      },
      "source": [
        "torch.all(torch.eq(a, a2))"
      ],
      "execution_count": 74,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(True)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 74
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JMEIlCUavhJn",
        "outputId": "f663c0e3-eddc-4500-e179-fe0f86973879"
      },
      "source": [
        "a = torch.rand(4, 3, 28, 28)\r\n",
        "a.shape"
      ],
      "execution_count": 76,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 3, 28, 28])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 76
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "vxIo0aFtwCaO",
        "outputId": "52fa2be3-d117-4890-ea2a-107f58b22cdf"
      },
      "source": [
        "a.transpose(1, 3).shape"
      ],
      "execution_count": 77,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 28, 28, 3])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 77
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ESketFoVwHpy",
        "outputId": "cc1c41f9-ffa8-4b9b-be95-d3ff9d7b6afa"
      },
      "source": [
        "b = torch.rand(4, 3, 28, 32)\r\n",
        "b.shape"
      ],
      "execution_count": 78,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 3, 28, 32])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 78
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "GXA1KYVlwMke",
        "outputId": "300d50f1-d11f-47e7-96a4-d12e213650bb"
      },
      "source": [
        "b.transpose(1, 3).shape"
      ],
      "execution_count": 79,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 32, 28, 3])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 79
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5bLzkt8wxlo1",
        "outputId": "d3d29910-1525-4891-ac24-6f0fb0235081"
      },
      "source": [
        "b.transpose(1, 3).transpose(1, 2).shape"
      ],
      "execution_count": 80,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 28, 32, 3])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 80
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "g4n3gDZ1xqQi",
        "outputId": "fc353a69-d6c3-4c8a-c7ee-b1d5ad01d28e"
      },
      "source": [
        "b.permute(0, 2, 3, 1).shape"
      ],
      "execution_count": 81,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([4, 28, 32, 3])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 81
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "IAMTxJlHxu-6"
      },
      "source": [
        " "
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}